package co.laomag.coffee.service.article.articleimpl;

import co.laomag.coffee.dao.article.ChDiscoveryDao;
import co.laomag.coffee.model.entity.ChDiscovery;
import co.laomag.coffee.model.vo.Message;
import co.laomag.coffee.model.vo.Pagination;
import co.laomag.coffee.service.article.ArticleService;
import co.laomag.coffee.utils.ResultUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 马泽朋
 * @version 1.0
 * @date 2020/4/18 下午 8:02
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ChDiscoveryDao chDiscoveryDao;
    /**
     * 发布发现文章
     *
     * @param chDiscovery 文章对象
     * @return int 大于0则发布成功
     */
    @Override
    public Message postDiscoveryArticle(ChDiscovery chDiscovery) {
        Message message = null;
        int insert = chDiscoveryDao.insert(chDiscovery);
        if (insert>0){
            message = ResultUtils.message(200, "发布成功");
        }else {
            message = ResultUtils.message(426, "发布失败");
        }
        return message;
    }

    /**
     * 获取发现文章并分类
     *
     * @param pagination 分页对象
     * @return 消息
     */
    @Override
    public Message getChDiscoveryList(Pagination pagination) {
        Message message = null;
        List<ChDiscovery> chDiscoveryList = chDiscoveryDao.getChDiscoveryList(pagination);
        int total = chDiscoveryDao.getTotal();
        if (chDiscoveryList == null || chDiscoveryList.size() < 0){
            message = ResultUtils.message(200, "暂无数据");
        }else {
            Map<String, Object> map = new HashMap<>(2);
            map.put("list", chDiscoveryList);
            map.put("total",total);
            message = ResultUtils.message(200, "查询成功",map);
        }
        return message;
    }

    /**
     * 更新发现文章
     *
     * @param record
     * @return
     */
    @Override
    public Message updateByPrimaryKeySelective(ChDiscovery record) {
        Message message = null;
        int i = chDiscoveryDao.updateByPrimaryKeySelective(record);
        if (i>0){
            message = ResultUtils.message(200, "修改成功");
        }else {
            message = ResultUtils.message(200, "修改失败");
        }
        return message;
    }

    /**
     * 删除指定发现文章
     *
     * @param discoveryId 发现文章id
     * @return
     */
    @Override
    public Message deleteFoundArticles(String discoveryId) {
        Message message = null;
        int i = chDiscoveryDao.deleteByPrimaryKey(discoveryId);
        if (i>0){
            message = ResultUtils.message(200, "删除成功");
        }else {
            message = ResultUtils.message(200, "删除失败");
        }
        return message;
    }
}
